package project.common;

import project.entity.po.User;
import project.entity.vo.UserVO;
import project.service.UserService;
import project.service.UserServiceImpl;
import sun.security.util.Password;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Scanner;

public class MainMenu {


    UserService userService = new UserServiceImpl();

    public void start() {
        mainMenu();
    }

    public void mainMenu() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("==========欢迎来到英雄联盟==========");
        System.out.println("0，登录");
        System.out.println("1，注册");
        System.out.println("2，展示所有召唤师");
        System.out.println("3，退出");
        System.out.println("4，查询召唤师");
        System.out.println("5，分页查询所有召唤师");
        System.out.println("6，测试，输出所有用户的创建时间");
        System.out.println("7，删除召唤师");
        System.out.println("召唤师，请输入你的选择:");

        int flag = scanner.nextInt();
        switch (flag) {
            case 0:
                login();
                break;
            case 1:
                regist();
                break;
            case 2:
                showAllUser();
                break;
            case 3:
                end();
                break;
            case 4:
                searchUserByName();
                break;
            case 5:
                pageList();
                break;
            case 6:
                test();
            case 7:
                deleteUser();
            default:
                mainMenu();
        }
    }

    private void deleteUser() {
        System.out.println("1，根据召唤师id删除  2，根据召唤师名字删除  0,返回主页面");
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        if (n == 1) {
            User user = getUserById();
            System.out.println("确定删除吗?  1,确定  2,我再想想");
            Integer z = scanner.nextInt();
            switch (z) {
                case 1:
                    deleteUserById(user.getId());
                    break;
                case 2:
                    System.out.println("您在考虑考虑");
                    deleteUser();
                default:
                    System.out.println("数据输入错误，请重新输入您的选项");
                    deleteUser();
                    break;
            }
        } else if (n == 2) {
            User user = getUserByName();
            System.out.println("确定删除吗?  1,确定  2,我再想想");
            Integer z = scanner.nextInt();
            switch (z) {
                case 1:
                    deleteUserByName(user.getName());
                    break;
                case 2:
                    System.out.println("您在考虑考虑");
                    deleteUser();
                default:
                    System.out.println("数据输入错误，请重新输入您的选项");
                    deleteUser();
                    break;
            }
        } else if (n == 3) {
            mainMenu();
        } else {
            System.out.println("数据输入错误，请重新输入您的选项");
            deleteUser();
        }
    }

    private void deleteUserById(Integer id) {
        userService.deleteUserById(id);
    }

    private void deleteUserByName(String name) {
        userService.deleteUserByName(name);
    }

    private void test() {
        userService.test();
    }

    //分页展示所有的召唤师
    private void pageList() {
        PageInfo pageInfo = userService.pageList(1, 3);
        System.out.println(pageInfo);
        pageListMenu(pageInfo);
    }

    //当执行pageList时的页面
    private void pageListMenu(PageInfo pageInfo) {
        System.out.println("请输入你想进行的操作：");
        System.out.println("0，返回到主页面");
        System.out.println("1，上一页");
        System.out.println("2，下一页");
        Scanner scanner = new Scanner(System.in);
        int flag = scanner.nextInt();
        switch (flag) {
            case 0:
                mainMenu();
                break;
            case 1:
                PageInfo prePage = prePage(pageInfo);
                System.out.println(prePage);
                pageListMenu(prePage);
                break;
            case 2:
                PageInfo nextPage = nextPage(pageInfo);
                System.out.println(nextPage);
                pageListMenu(nextPage);
                break;
            default:
                pageListMenu(pageInfo);
        }
    }

    //分页查询全部，下一页
    private PageInfo nextPage(PageInfo pageInfo) {
        if (pageInfo.getHasNext()) {
            PageInfo pageInfo1 = userService.pageList(pageInfo.getPageNum() + 1, pageInfo.getPageSize());
            return pageInfo1;
        }
        System.out.println("当前页已经是最后一页啦");
        return pageInfo;
    }

    //分页查询全部，上一页
    private PageInfo prePage(PageInfo pageInfo) {
        if (pageInfo.getHasPrev()) {
            PageInfo pageInfo1 = userService.pageList(pageInfo.getPageNum() - 1, pageInfo.getPageSize());
            return pageInfo1;
        }
        System.out.println("当前页已经是第一页啦");
        return pageInfo;
    }

    //根据名字查找召唤师
    private void searchUserByName() {
        getUserByName();
        mainMenu();
    }

    //根据id查找召唤师
    private void searchUserById() {
        getUserById();
        mainMenu();
    }

    private User getUserByName() {
        System.out.println("请输入召唤师名字：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        User userByName = userService.getUserByName(name);
        if (Objects.isNull(userByName)) {
            System.out.println("该召唤师不存在");
        } else {
            System.out.println(userByName);
        }
        return userByName;
    }

    //根据id查找召唤师，并且返回召唤师对象
    private User getUserById() {
        System.out.println("请输入召唤师的id");
        Scanner scanner = new Scanner(System.in);
        Integer id = scanner.nextInt();
        User userById = userService.getUserById(id);
        if (Objects.isNull(userById)) {
            System.out.println("该召唤师不存在");
        } else {
            System.out.println(userById);
        }
        return userById;
    }

    //展示所有的召唤师信息,不分页
    private void showAllUser() {
        List<UserVO> list = new ArrayList<>();
        UserService userService = new UserServiceImpl();
        list = userService.getAllUsers();
        list.stream().forEach(System.out::println);
        mainMenu();
    }

    private void show() {

    }

    //退出功能
    private void end() {
        System.out.println("大爷下次还来玩啊");
        System.exit(0);
    }

    //登录功能,进行登录信息检查
    private void login() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();

        Boolean flag = userService.loginCheck(name, password);
        if (flag)
            gameMenu(name, password);
        else {
            System.out.println("账号或密码不正确，请重新登录");
            mainMenu();
        }
    }

    //登录完成，进去的界面
    private void gameMenu(String name, String password) {
        System.out.println("==============================");
        System.out.println("登陆成功,这是游戏界面");
        System.out.println("请输入你想进行的操作  1，修改密码  2，重新登录");
        Scanner scanner = new Scanner(System.in);
        Integer n = scanner.nextInt();
        switch (n) {
            case 1:
                changePassword(name, password);
                break;
            case 2:
                mainMenu();
                break;
            default:
                gameMenu(name, password);
                break;
        }
        System.out.println("==============================");
    }

    private void changePassword(String name, String password) {
        System.out.println("请输入原密码");
        Scanner scanner = new Scanner(System.in);
        String oldPassword = scanner.next();
        if (oldPassword.equals(password)) {
            System.out.println("请输入新密码");
            String newPassword = scanner.next();
            userService.changePassword(name, newPassword);
            System.out.println("修改密码成功，请重新登录");
            mainMenu();
        } else {
            System.out.println("密码不正确，请重新输入");
            gameMenu(name, oldPassword);
        }
    }

    //注册功能
    private void regist() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入你想要的召唤师名字(不能包含空格):");
        String name = scanner.nextLine();
        System.out.println("请输入设置的密码:");
        String password = scanner.nextLine();

        //需要一个注册功能
        int regist = userService.regist(new User(name, password));

        if (regist > 0) {
            System.out.println("注册成功，请立即登录");
        } else {
            System.out.println("该召唤师名已被注册");
        }
        mainMenu();
    }
}
